package com.wujilee.mqdelaytask.service;

import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.wujilee.mqdelaytask.common.DelayTaskStatus;
import com.wujilee.mqdelaytask.entity.EntityDelayTask;
import com.wujilee.mqdelaytask.mapper.EntityDelayTaskGeneratedMapper;
import com.wujilee.mqdelaytask.mq.entity.DelayTask;
import com.wujilee.mqdelaytask.mq.entity.Task;
import com.wujilee.mqdelaytask.service.inform.SendTaskService;
import com.wujilee.mqdelaytask.service.mq.MqSender;
import org.springframework.boot.autoconfigure.condition.ConditionalOnBean;
import org.springframework.scheduling.Trigger;
import org.springframework.scheduling.TriggerContext;
import org.springframework.scheduling.concurrent.ThreadPoolTaskScheduler;
import org.springframework.scheduling.support.CronTrigger;
import org.springframework.scheduling.support.PeriodicTrigger;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.Date;
import java.util.concurrent.TimeUnit;
import java.util.function.Consumer;

/**
 * @author liwuji
 * @date 2024/4/12
 * @desc
 **/
@ConditionalOnBean(ExecuteDelayTaskService.class)
@Service
public class ExecuteDelayTaskServiceImpl implements ExecuteDelayTaskService {

    @Resource
    private EntityDelayTaskGeneratedMapper delayTaskGeneratedMapper;
    @Resource
    private MqSender mqSender;
    @Resource
    private SendTaskService sendTaskService;
    
    @Resource
    private ThreadPoolTaskScheduler threadPoolTaskScheduler;
  
    
    @Override
    public void execute(EntityDelayTask entityDelayTask) {
        // 第一次push
        // 第二次push
        // 第三次push
        threadPoolTaskScheduler.schedule(()->{
            
        },new Trigger(){
            @Override
            public Date nextExecutionTime(TriggerContext triggerContext){
                new PeriodicTrigger(Long.parseLong(task.getScheduleContent()), TimeUnit.MILLISECONDS);
                pt.setFixedRate(false);
                return new CronTrigger("0/10 * * * * *").nextExecutionTime(triggerContext);
            }
        });

        
        
        
        
        LambdaUpdateWrapper<EntityDelayTask> updateWrapper = new LambdaUpdateWrapper<>();
        updateWrapper.set(EntityDelayTask::getStatus, DelayTaskStatus.RUNNING).eq(EntityDelayTask::getId, entityDelayTask.getId()).eq(EntityDelayTask::getStatus, entityDelayTask.getStatus());
        delayTaskGeneratedMapper.update(null, updateWrapper);
        
        
        Task task = new Task();
        task.setTaskId(entityDelayTask.getId());
        task.setParam(entityDelayTask.getParam());
        task.setMessage(entityDelayTask.getMessage());
        sendTaskService.sendTask(task);
        
        LambdaUpdateWrapper<EntityDelayTask> updateWrapper1 = new LambdaUpdateWrapper<>();
        updateWrapper1.set(EntityDelayTask::getStatus, DelayTaskStatus.SUCCESS).eq(EntityDelayTask::getId, entityDelayTask.getId()).eq(EntityDelayTask::getStatus, entityDelayTask.getStatus());
        delayTaskGeneratedMapper.update(null, updateWrapper);
    }

    @Override
    public void successExecute(Consumer consumer) {

    }

    @Override
    public void failExecute(Consumer consumer) {

    }

    @Override
    public Consumer<EntityDelayTask> execute() {
        Consumer<EntityDelayTask> consumer = executeDelayTask -> {
            mqSender.send(executeDelayTask);
        };
        return null;
    }
}
